
<template>
  <div class="role-container">
   <el-card class="box-card">
    <el-row style="margin: 10px">
     <el-col :span="24">
          <el-button type="primary" size="small" icon="el-icon-plus" 
          @click="addClick"
            >添加</el-button
          >
          <el-button
            type="danger"
            size="small"
            icon="el-icon-delete"
            @click="deleteClick"
            >批量删除</el-button
          >
        </el-col>
      </el-row>
      <!-- 表格 -->
      <el-table :data="roleList" style="width: 100%" stripe  lazy border @selection-change="handleSelectionChange">>
        <el-table-column
      type="selection"
      width="55">
    </el-table-column>
        <el-table-column prop="name" label="角色名称" width="130" align="center"> </el-table-column>
        <el-table-column prop="stutas" label="状态" width="80" align="center">
          <template v-slot="slot">
            <el-switch v-model="slot.row.status" @change="disable(slot.row)"></el-switch>
            <!-- <el-tag type="success" v-if="slot.row.status">正常</el-tag> -->
            <!-- <el-tag v-else>限制</el-tag> -->
          </template>
        </el-table-column>
        <el-table-column prop="is_admin" label="默认角色" width="80" align="center">
          <template v-slot="slot">
            <i v-if="slot.row.is_default" class="el-icon-check"></i>
            <i v-else class="el-icon-close"></i>
          </template>
        </el-table-column>
        <el-table-column
          prop="createdAt"
          label="新增时间"
          width="160"
          align="center"
        >
        </el-table-column>
        <el-table-column prop="address" label="操作"  align="center">
          <template v-slot="slot">
             <el-button type="warning" icon="el-icon-view" size="small"
            @click="setDefaultRole(slot.row.id)"
              >设为默认角色</el-button
            >
            <el-button type="primary" icon="el-icon-edit" size="small"
            @click="editClick(slot.row)"
              >编辑</el-button
            >
            <el-button type="danger" icon="el-icon-delete" size="small"
             @click="deleteClick(slot.row)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <el-pagination
      background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pageNum"
        :page-sizes="[5, 10, 15, 20]"
        :page-size="queryInfo.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
      <!-- 添加抽屉 -->
        <el-drawer
        title="添加角色"
        :visible.sync="adddrawer"
        :direction="direction"
        :before-close="handleClose"
        close-on-press-escape
        @close="close"
      >
        <el-form
          :label-position="labelPosition"
          label-width="40px"
          :model="addRoleform"
          size="small"
        >
          <el-form-item label="名称">
            <el-input v-model="addRoleform.name"></el-input>
          </el-form-item>
          <el-form-item label="状态">
            <el-switch v-model="addRoleform.status"> </el-switch>
          </el-form-item>
          <el-form-item>
           <el-button size="small" type="primary" @click="addRole">添加</el-button>
             <el-button size="small" type="primary" @click="close">重置</el-button>
          </el-form-item>
            
        </el-form>
      </el-drawer>
       <!-- 编辑抽屉 -->
        <el-drawer
        title="编辑菜单"
        :visible.sync="editdrawer"
        :direction="direction"
        :before-close="handleClose"
        close-on-press-escape
        @close="close"
      >
        <el-form
          :label-position="labelPosition"
          label-width="40px"
          :model="editRoleform"
          size="small"
        >
          <el-form-item label="名称">
            <el-input v-model="editRoleform.name"></el-input>
          </el-form-item>
          <el-form-item label="状态">
             <el-switch v-model="editRoleform.status"> </el-switch>
          </el-form-item>
          
          <el-form-item>
           <el-button size="small" type="primary" @click="editRole">修改</el-button>
             <el-button size="small" type="primary" @click="close">重置</el-button>
          </el-form-item>
            
        </el-form>
      </el-drawer>
    </el-card>
  </div>
</template>

<script>
import { getRoleList,deleteRole,addRole,setDefaultRole,getRoleId,editRole } from '@/api/role';
export default {
  name: 'Role',
data() {
    return {
      roleList: [],// 文章表格数据
       queryInfo: {
        query: '', // 查询参数
        pageNum: 1,// 分页页码
        pageSize: 10// 页面数据条数
      },
      total: 0,
      selectRows: [],// 全选数据
       adddrawer:false,
       editdrawer:false,
       direction: "rtl",
       labelPosition: "left",//改变表单域标签的位置
       options: [{
          value: '1',
          label: '正常'
        }, {
          value: '0',
          label: '限制'
        }],
       editRoleform:{},
       addRoleform:{
         name:'',
         status:0
       },
    };
  },
  created() {
    this.getRoleList();
  },
  methods: {
    // 滑块功能
    async disable(row) {
      const { id, status } = row;
      const res = await editRole(id, { status: status });
      this.$message.success(`${status ? "正常" : "限制"}`);
    },
    //修改角色信息
   async editRole(){
     const id = this.editRoleform.id
     const body = this.editRoleform
    await editRole(id, body)
    this.getRoleList();
    this.editdrawer = !this.editdrawer
    this.$message.success('修改成功')
    },
        // // 点击编辑 弹出抽屉
    async editClick(row){
      const res =await getRoleId(row.id)
      this.editRoleform = res
      this.editdrawer = !this.editdrawer
    },
    // 设置默认角色
    async setDefaultRole(id){
      await setDefaultRole(id)
      this.getRoleList();
      this.$message.success('设置默认角色成功');
    },
    // 添加角色
    async addRole(){
     const res =  await addRole(this.addRoleform)
      this.getRoleList();
      this.adddrawer = !this.adddrawer;
      this.$message.success('添加成功')
    },
    // 点击添加 弹出抽屉
    addClick(){
      this.adddrawer = !this.adddrawer;
    },
    // 关闭抽屉提示
    handleClose(done) {
      done();
      // this.$confirm("确认关闭？")
      //   .then((_) => {
      //     done();
      //   })
      //   .catch((_) => {});
    },
    // 关闭抽屉就清空
    close(){
      this.editRoleform = {}
      this.addRoleform={
         name:'',
         status:0
       }
    },
    // 文章表格数据
    async getRoleList() {
      const { list, pageNum, pageSize, total } = await getRoleList(this.queryInfo);
      this.roleList = list;
      this.queryInfo.pageNum =Number(pageNum);
      this.queryInfo.pageSize =Number(pageSize);
      this.total = total;
    },
    // 设置页面数据条数
    handleSizeChange(val) {
      this.queryInfo.pageSize = val
      this.getRoleList();
    },
    // 设置分页页码
    handleCurrentChange(val) {
      this.queryInfo.pageNum=val
      this.getRoleList();
    },
    // 删除角色
    deleteClick(row){
    if (row.id) {
        this.$confirm("你确定要删除选中项吗", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        })
          .then(async () => {
            await deleteRole({ ids: [row.id] });
            this.getRoleList();
            this.$message.success("删除成功");
          })
          .catch(() => {
            this.$message.info("已取消删除");
          });
      } else {
        // 批量删除
        if (!this.selectRows.length)
          return this.$message.warning("请选择要删除的选中项");
        const ids = this.selectRows.map((item) => item.id);
        this.$confirm("你确定要删除选中项吗", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        })
          .then(async () => {
            await deleteRole({ ids });
            this.getRoleList();
            this.$message.success("删除成功");
          })
          .catch(() => {
            this.$message.info("已取消删除");
          });
      }
    },
    // 全选数据
    handleSelectionChange(val) {
      this.selectRows=val
      }
  },
};
</script>

<style lang="scss">
.box-card {
  margin: 20px;
}
.el-table{
  margin-bottom: 20px;
}
.el-image{
  border-radius: 50%;
}
.el-form{
  margin: 40px 8px;
}
</style>
